package cn.edu.zhku.citrusmonitor.baseinfo.disease.service;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;

import com.mysql.jdbc.Connection;

import cn.edu.zhku.citrusmonitor.baseinfo.disease.dao.DiseaseDao;
import cn.edu.zhku.citrusmonitor.baseinfo.disease.dao.DiseaseDaoImpl;
import cn.edu.zhku.citrusmonitor.domain.Bean;
import cn.edu.zhku.citrusmonitor.domain.TbDisease;
import cn.edu.zhku.citrusmonitor.util.Message;
import cn.edu.zhku.citrusmonitor.util.UUIDUtils;
import cn.edu.zhku.citrusmonitor.util.xy.PageBean;
import net.sf.json.JSONObject;

public class DiseaseServiceImpl implements DiseaseService{
	DiseaseDao DD = new DiseaseDaoImpl();
	
	@Override
	public JSONObject modifyDisease(TbDisease disease) {
		try {
			if(DD.modifyDiseaseByDiseaseBean(disease) != 0) {
				return JSONObject.fromObject(new Message("1","修改成功"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(new Message("2","修改失败"));
	}

	@Override
	public JSONObject removeDisease(TbDisease disease) {
		try {
			if(DD.removeDiseaseByDiseaseId(disease.getDiseaseId())!=0) {
				return JSONObject.fromObject(new Message("1","删除成功"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(new Message("2","删除失败"));
	}

	@Override
	public JSONObject addDisease(TbDisease disease) {
		disease.setDiseaseId(UUIDUtils.getId());
		try {
			if(DD.addDiseaseByDiseaseBean(disease) !=0) {
				return JSONObject.fromObject(new Message("1","添加成功"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(new Message("2","添加失败"));
	}

	@Override
	public PageBean<Bean> findDiseasePageBean(Integer currentPage, Integer pageSize) {
		long count = 0;
		List<Bean> list = null;
		PageBean<Bean> pbn = new PageBean<>();
		try {
			count = DD.getCount();
			list = DD.getDiseaseBeanList(currentPage, pageSize);
			pbn.setList(list);
			pbn.setCurrentPage(currentPage);//当前页
			pbn.setPageSize(pageSize);	//每页显示条数
			pbn.setCount(count);	//全部病害总数
			pbn.setTotalPage((long) pbn.getTotalPage());	//在PageSIze下，总共页数
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return pbn;
	}

	@Override
	public List<TbDisease> getDisease(String diseaseId) {
		List<TbDisease> list = null;
		try {
			list = DD.getDiseaseById(diseaseId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@Override
	public Bean showDisease(String id) {
		Bean bean = null;
		try {
			bean = DD.showDiseaseById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bean;
	}

	@Override
	public List<Bean> findRecentDisease(Integer pageSize, Bean bean) {
		List<Bean> list = null;
		try {
			list = DD.getRecentPestBeanList(pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public PageBean<TbDisease> findAllDiseaseDetail(Integer currentPage, Integer pageSize) {
		long count = 0;
		List<TbDisease> list = null;
		PageBean<TbDisease> pbn = new PageBean<>();
		try {
			count = DD.getCount();
			list = DD.getDiseaseDetailBeanList(currentPage, pageSize);
			pbn.setList(list);
			pbn.setCurrentPage(currentPage);//当前页
			pbn.setPageSize(pageSize);		//每页条数
			pbn.setCount(count);;		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pbn;
	}

	@Override
	public PageBean<TbDisease> findAllDiseaseDetailByWord(Integer currentPage, Integer pageSize, String word) {
		List<TbDisease> list = null;
		Long count = null;
		PageBean<TbDisease> pbn = new PageBean<>();
		pbn.setCurrentPage(currentPage);//当前页
		pbn.setPageSize(pageSize);		//每页条数
		if(word != "") {
			try {
				count  = DD.searchDiseasetCountByWord(word);
				list = DD.searchDiseaseByWord(currentPage, pageSize, word);
			}catch (SQLException e) {
				e.printStackTrace();
			}
			pbn.setList(list);
			pbn.setCount(count);		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		}else {
			count = (long) 0;
			pbn.setList(list);
			pbn.setCount(count);		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
	}
		return pbn;
	}

	@Override
	public JSONObject delSelect(String[] ids) {
		Connection conn = null;
		try {
			if(DD.delSelect(ids).length > 0) {
				return JSONObject.fromObject(new Message("1","删除成功"));
			}
		} catch (SQLException e) {
			try {
				DbUtils.rollback(conn);	//进行事务回滚
				} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				DbUtils.close(conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return JSONObject.fromObject(new Message("2","删除成功"));
	}

}
